﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hack.Attributes;
using Hack.Extensions;
using Hack.Properties;
namespace Hack
{
    [OrginalAuthor(Url = "http://signum.codeplex.com")]
    public struct DateSpan
    {
        public readonly int Years;
        public readonly int Months;
        public readonly int Days;

        public DateSpan(int years, int months, int days)
        {
            this.Years = years;
            this.Months = months;
            this.Days = days;
        }

        public static DateSpan FromToDates(DateTime min, DateTime max)
        {
            if (min > max) return FromToDates(max, min).Invert();

            int yeas = max.Year - min.Year;
            int months = max.Month - min.Month;


            if (max.Day < min.Day)
                months -= 1;

            if (months < 0)
            {
                yeas -= 1;
                months += 12;
            }

            int days = max.Subtract(min.AddYears(yeas).AddMonths(months)).Days;

            return new DateSpan(yeas, months, days);

        }

        public DateTime AddTo(DateTime date)
        {
            return date.AddDays(Days).AddMonths(Months).AddYears(Years);
        }

        public DateSpan Invert()
        {
            return new DateSpan(-Years, -Months, -Days);
        }

        public override string ToString()
        {
            return Resources._0Year1Month2Day.Formato(Years, Months, Days);
        }
    }
}
